home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 6984 / 6984.xpi / chrome / lazarus.jar / content / text-manager.js < prev    next >
Text File  |  2009-11-24  |  13KB  |  336 lines

  1.  
  2.  
  3. //load translations from the browser
  4. Lazarus.getString = Lazarus.getBrowser().Lazarus.getString;
  5. Lazarus.db = Lazarus.getBrowser().Lazarus.db;
  6. Lazarus.decrypt = Lazarus.getBrowser().Lazarus.decrypt;
  7. Lazarus.hashQuery = Lazarus.getBrowser().Lazarus.hashQuery;
  8.  
  9.  
  10. LazarusTextManager = {
  11.  
  12.     /**
  13.     * steup this window
  14.     */
  15.     init: function(){
  16.         window.removeEventListener("load", LazarusTextManager.init, true);
  17.         Lazarus.$('tree-search').addEventListener("keydown", LazarusTextManager.onSearchKeyDown, true);
  18.         LazarusTextManager.initTree();
  19.         if (Lazarus.getPref('extensions.lazarus.disableSearch')){
  20.             //disable the search functionality
  21.             Lazarus.$('tree-search').disabled = true;
  22.             Lazarus.$('message-search').hidden = true;
  23.             //and show the "search is disabled" message 
  24.             Lazarus.$('message-search-disabled').hidden = false;
  25.         }
  26.         window.sizeToContent();
  27.     },
  28.     
  29.     
  30.     /**
  31.     * setup the XULTree
  32.     */
  33.     initTree: function(){
  34.         var tree = Lazarus.$('tree-text');
  35.         var sorter = new Tree.Sorter(tree);
  36.         
  37.         //we need to build an nsiTreeView object to tell the tree how to display the fields
  38.         tree.nsITreeView = function(data){
  39.             this.rowCount = data.length;
  40.             this.getCellText = function(row, col){
  41.                 
  42.                 switch (col.id){  
  43.                     case "created":
  44.                         return Lazarus.formatDate(new Date(data[row]["created"] * 1000));
  45.                     
  46.                     case "url":
  47.                         if (!data[row]["url"]){
  48.                             data[row]["url"] = Lazarus.decrypt(tree.data[row]["url_encrypted"]);
  49.                         }
  50.                         return data[row]["url"];
  51.                         
  52.                     case "text":
  53.                         //cache the results, as decryption is CPU intensive
  54.                         if (!data[row]["summary"]){
  55.                             data[row]["summary"] = Lazarus.decrypt(data[row]["summary_encrypted"]) || ("["+ Lazarus.getString("untitled") +"]");
  56.                         }
  57.                         return data[row]["summary"];
  58.                         
  59.                     default:
  60.                         return data[row][col.id];
  61.                 }
  62.             };
  63.             
  64.             this.getCellValue = function(row, col){
  65.                 return data[row][col.id];
  66.             };
  67.             
  68.             this.setCellValue = function(row, col, val){
  69.                 switch (col.id){    
  70.                     case "check":
  71.                         data[row][col.id] = (val == "true") ? true : false;
  72.                         LazarusTextManager.refreshDeleteSelectedButton();            
  73.                         return;
  74.                 }
  75.             };
  76.             this.setTree = function(treebox){this.treebox = treebox;};
  77.             this.isEditable = function(row, col){
  78.                 return col.editable;
  79.             }
  80.             this.getImageSrc = function(row, col){
  81.                 switch (col.id){
  82.                     case "recoverText":
  83.                         return 'chrome://lazarus/skin/recover-form.png';
  84.                         
  85.                     default:
  86.                         return null;
  87.                 }
  88.             }
  89.             this.isContainer = function(row){return false;};
  90.             this.isSeparator = function(row){return false;};
  91.             this.isSelectable = function(){};
  92.             this.isSorted = function(){return false;};
  93.             this.getLevel = function(row){return 0;};
  94.             this.getRowProperties = function(row, props){};
  95.             this.getCellProperties = function(row, col, props){     
  96.                 switch (col.id){
  97.                     //make the formURL look like a text-link
  98.                     case "url":
  99.                         var aserv=Components.classes["@mozilla.org/atom-service;1"].getService(Components.interfaces.nsIAtomService);
  100.                         props.AppendElement(aserv.getAtom("lazarustextlink"));
  101.                         return "lazarustextlink";
  102.                         break;
  103.                     
  104.                     default:
  105.                         break;
  106.                 }
  107.             };
  108.             this.getColumnProperties = function(colid, col, props){};
  109.             this.cycleHeader = function(col, elem){};
  110.             this._getDataRowById = function(id){
  111.                 for (var i=0; i<data.length; i++){
  112.                     if (data[i]["id"] == id){
  113.                         return data[i];
  114.                     }
  115.                 }
  116.                 return null;
  117.             }
  118.         }
  119.         
  120.         //now we need to get some data and insert it into the tree
  121.         //load the list of text and forms from the database
  122.                 var query = "SELECT id, url_encrypted, created, summary_encrypted, 'textdata' as `table` FROM textdata UNION ALL SELECT id, formurl, created, formtext, 'forms' FROM forms ORDER BY created DESC";
  123.  
  124.                 var rs = Lazarus.db.rs(query);
  125.         
  126.         tree.initData(rs);
  127.         tree.columns.restoreNaturalOrder();
  128.         
  129.         tree.addEventListener("click", function(event){
  130.             //REF: http://developer.mozilla.org/en/docs/Code_snippets:Tree
  131.             // get the row, col and child element at the point
  132.             var row = {}, col = {}, child = {};
  133.             var tbo = tree.treeBoxObject;
  134.             tbo.getCellAt(event.clientX, event.clientY, row, col, child);
  135.             //get the colId and row
  136.             //user clicked a column header
  137.             if (!col.value){return}
  138.             var colId = (col.value && typeof col.value == "string") ? col.value : col.value.id;
  139.             var rowNum = row.value;
  140.             //make sure we clicked on a valid row
  141.             if (rowNum < 0 || rowNum >= tree.data.length){return}
  142.             tree.onCellClick(rowNum, colId, child);
  143.         }, true);
  144.         
  145.         /**
  146.         * handle click within the tree
  147.         */
  148.         tree.onCellClick = function(row, colId, child){
  149.             
  150.             switch(colId){
  151.                 case "url":
  152.                     //navigate to given url
  153.                     if (!tree.data[row]["url"]){
  154.                         tree.data[row]["url"] = Lazarus.decrypt(tree.data[row]["url_encrypted"]);
  155.                     }
  156.                     var url = tree.data[row]["url"];
  157.                     Lazarus.getBrowser().Lazarus.openURL(url, true, true);
  158.                     //and close the dialog
  159.                     //NO, don't close, they may have got the wrong url 
  160.                     //window.close();
  161.                     return;
  162.                     
  163.                 case "recoverText":
  164.                     window.openDialog("chrome://lazarus/content/text-recover.xul?id="+ tree.data[row]["id"] +"&table="+ tree.data[row]["table"], "lazarusTextRecover", "chrome,modal,centerscreen,dialog=no");
  165.                     return;
  166.                     
  167.                 default:
  168.                     //let the tree select this row.
  169.                     return;
  170.             }
  171.         }
  172.     },
  173.     
  174.     onSearchKeyDown: function(evt){
  175.         
  176.         //stop the dialog from closing when you hit [enter] or escape?
  177.         if (evt.keyCode == 13){
  178.             evt.stopPropagation();
  179.             evt.preventDefault();
  180.         }
  181.         
  182.         //do a search as soon as the user stops typing.
  183.         if (LazarusTextManager.timerRunSearch){
  184.             clearTimeout(LazarusTextManager.timerRunSearch);
  185.         }
  186.         LazarusTextManager.timerRunSearch = setTimeout(LazarusTextManager.runSearch, 250);
  187.     },
  188.     
  189.     
  190.     /**
  191.     * 
  192.     */
  193.     runSearch: function(){
  194.         
  195.         var rs;
  196.         var query = Lazarus.trim(Lazarus.$('tree-search').value);
  197.                 var sortDirection = Lazarus.$('created').getAttribute("sortDirection");
  198.                 
  199.         if (query){
  200.             var hashedQuery = Lazarus.hashQuery(query);
  201.                         
  202.                         //we should really join the two tables, but that's going to have to wait until Lazarus 3.0 (to much work at this point)
  203.             rs = Lazarus.db.rs("SELECT id, url_encrypted, created, summary_encrypted, 'textdata' as `table` FROM textdata JOIN textdata_fulltext ON textdata.id = textdata_fulltext.docid WHERE textdata_fulltext.hashed_text MATCH ?1", hashedQuery);
  204.                         //and search forms as well
  205.                         var rsForms = Lazarus.db.rs("SELECT id, formurl as url_encrypted, created, formtext as summary_encrypted, 'forms' as `table` FROM forms JOIN forms_fulltext ON forms.id = forms_fulltext.docid WHERE forms_fulltext.hashed_text MATCH ?1", hashedQuery);
  206.                         
  207.                         //and join the forms with the text
  208.                         rs = rsForms ? rs.concat(rsForms) : rs;
  209.                         
  210.                         //and sort in the correct order
  211.                         rs.sort(function(a, b){
  212.                             return (a.created > b.created) ? 1 : -1;
  213.                         });
  214.                         
  215.                         //if sort is by date desc, then we need to invert the Array
  216.                         if (sortDirection == "descending"){
  217.                             rs.reverse();
  218.                         }
  219.         }
  220.         else {
  221.                         var desc = (sortDirection == "descending") ? " DESC" : "";
  222.             rs = Lazarus.db.rs("SELECT id, url_encrypted, created, summary_encrypted, 'textdata' as `table` FROM textdata UNION ALL SELECT id, formurl, created, formtext, 'forms' FROM forms ORDER BY created"+ desc);
  223.         }
  224.         
  225.         //highlight the warning about using full words to search for stuff if there are no results
  226.         var classname = (rs.length == 0) ? "warning" : "";
  227.         Lazarus.$('message-box').setAttribute("class", classname);
  228.         
  229.         var tree = Lazarus.$('tree-text');
  230.         tree.data = rs;
  231.         tree.refresh(); 
  232.         LazarusTextManager.refreshClearSearchButton();
  233.     },
  234.     
  235.     
  236.     /**
  237.     * toggle all of the checkboxes in the tree on or off.
  238.     */
  239.     toggleAll: function(){
  240.         var checked = !Lazarus.$('select-all').checked;
  241.         var tree = Lazarus.$('tree-text');
  242.         for (var i=0; i<tree.data.length; i++){
  243.             tree.data[i]["check"] = checked;
  244.         }
  245.         Lazarus.$('select-all').checked = checked;
  246.         LazarusTextManager.refreshDeleteSelectedButton();
  247.     },
  248.     
  249.     
  250.     /**
  251.     * "delete selected" button pushed 
  252.     */
  253.     onDeleteSelected: function(){
  254.         //do we have any selected textdata?
  255.         var ids = LazarusTextManager.getSelectedIds();
  256.         if (ids.length == 0){
  257.             //shouldn't be able to get here
  258.             alert(Lazarus.getString("LazarusTextManager.noItemsSelected"));
  259.         }
  260.         else if (confirm(Lazarus.getString("LazarusTextManager.confirmDelete"))){
  261.                         var forms = [];
  262.                         var texts = [];
  263.                         
  264.                         for(var i=0; i<ids.length; i++){
  265.                             if (ids[i].table == "textdata"){
  266.                                 texts.push(ids[i].id);
  267.                             }
  268.                             else if (ids[i].table == "forms"){
  269.                                 forms.push(ids[i].id);
  270.                             }
  271.                             else {
  272.                                 throw Error("Unknown table type ["+ ids[i].table +"]");
  273.                             }
  274.                         } 
  275.                 
  276.             //remove the forms, and re-search
  277.                         if (texts.length > 0){
  278.                             Lazarus.db.exe("DELETE FROM textdata WHERE id IN ("+ texts.join(",") +")");
  279.                             Lazarus.db.exe("DELETE FROM textdata_fulltext WHERE docid IN ("+ texts.join(",") +")");    
  280.                         }
  281.                         
  282.                         if (forms.length > 0){
  283.                             Lazarus.db.exe("DELETE FROM forms WHERE id IN ("+ forms.join(",") +")");
  284.                             Lazarus.db.exe("DELETE FROM forms_fulltext WHERE docid IN ("+ texts.join(",") +")");    
  285.                         }
  286.             
  287.             LazarusTextManager.runSearch();
  288.         }
  289.     },
  290.     
  291.     /**
  292.     * clear the searchbox
  293.     */
  294.     clearSearch: function(){
  295.         Lazarus.$('tree-search').value = "";
  296.         Lazarus.$('tree-text').filterData();
  297.     },
  298.     
  299.     
  300.     /**
  301.     * enable/disable the clear search button
  302.     */
  303.     refreshClearSearchButton: function(){
  304.         //Lazarus.$('clear-search').disabled = Lazarus.$('tree-search').value ? false : true;
  305.     },
  306.     
  307.     
  308.     /**
  309.     * enable/disable the clear search button
  310.     */
  311.     refreshDeleteSelectedButton: function(){
  312.         Lazarus.$('delete-selected').disabled = (LazarusTextManager.getSelectedIds().length == 0);
  313.     },
  314.     
  315.     
  316.     /**
  317.     * return an array of id's of the currently selected textdata items
  318.     */
  319.     getSelectedIds: function(){
  320.         var ids = [];
  321.         var data = Lazarus.$('tree-text').data;
  322.         for (var i=0; i<data.length; i++){
  323.             if (data[i]["check"]){
  324.                 ids.push({id:data[i]["id"], table:data[i]["table"]});
  325.             }
  326.         }
  327.         return ids;
  328.     }
  329.  
  330. }
  331.  
  332.  
  333.  
  334.  
  335.  
  336. window.addEventListener("load", LazarusTextManager.init, true);